//index.js
const app = getApp()

Page({
  data: {
    // 用户信息
    userInfo: {},
    // 是否登陆
    isLogin: false,
    nowPage: 0,
    // 导航栏背景色
    navBackground: ['#fba414', '#fa7e23', '#F55F3B'],
    searchBackground: '',
    swTop: 0,
    // 底部导航配置信息
    tabbarList: [{
        text: '首页',
        iconPath: '../../../navIcon/index.png',
        selectedIconPath: '../../../navIcon/index-active.png'
      },
      {
        text: '搜索',
        iconPath: '../../../navIcon/search.png',
        selectedIconPath: '../../../navIcon/search-active.png'
      },
      {
        text: '我的',
        iconPath: '../../../navIcon/personal.png',
        selectedIconPath: '../../../navIcon/personal-active.png'
      }
    ],
    // scroll-view下拉刷新信息
    refreshState: [
      false,
      false,
      false
    ],
    // my组件
    myMyComponent: '',
    // 首页组件
    myMainComponent: '',
    // 搜索页组件
    mySearchComponent:'',
    // 是否为首次加载页面
    firstLoad:true
  },
  onLoad(options) {
    // 获取当前分页
    if (options.page) {
      this.setData({
        nowPage: options.page
      })
    }else{
      // 如果没有设置page，则代表为首页，swiperChange不会触发
        this.setData({
          firstLoad:false
        });
    }
    // 检测用户是否登陆
    this._checkSession();
  },
  // 检测用户是否登陆
  _checkSession() {
    wx.checkSession({
      success: (res) => {
        // 已经登陆，获取用户数据
        if (wx.getStorageSync('userInfo')) {
          this.setData({
            userInfo: wx.getStorageSync('userInfo'),
            isLogin: true
          })
        }
      }
    })
  },
  onShow(){
    this.loadData(this.data.nowPage);
  },
  onReady() {
    // 计算导航栏高度并赋值
    setTimeout(() => {
      let query = wx.createSelectorQuery();
      query.select('.navBar').boundingClientRect(rect => {
        let height = rect.height;
        this.setData({
          swTop: height
        })
      }).exec();
    }, 300);
    // 获取组件实例
    this.setData({
      myMyComponent: this.selectComponent('#myComponent'),
      myMainComponent: this.selectComponent('#mainComponent'),
      mySearchComponent: this.selectComponent('#searchComponent'),
    });
    // 获取页面数据
    this.loadData(this.data.nowPage);
  },
  // 加载数据
  loadData(page) {
    // 首页
    if (page == 0) {
      this.data.myMainComponent.getMainData();
    } else if (page == 1) {
      this.data.mySearchComponent.getSearchData();
    }
    // 我的页面
    else {
      this.data.myMyComponent.getMyData();
    }
  },
  // 监听下拉
  diyPull(e) {
    // if(e.detail.dy>=100){
    //   console.log(e)
    // }
    // this.data.refreshState[this.data.nowPage] = true;
    // this.setData({
    //   refreshState:this.data.refreshState
    // })
  },
  // 更新数据
  updatePull() {
    this.loadData(this.data.nowPage);
    setTimeout(()=>{
      this.data.refreshState[this.data.nowPage] = false;
      this.setData({
        refreshState: this.data.refreshState
      })
    },2000);
  },
  goSearch() {
    wx.showModal({
      cancelColor: 'cancelColor',
      content: '该功能还在建设中'
    });
  },
  // 当swiper发生变化的时候
  swiperChange(e) {
    // 判断是否为首次加载，防止数据加载多次
    if(this.data.firstLoad){
      this.setData({
        firstLoad:false
      });
      return;
    }
    this.loadData(e.detail.current)
    console.log('swiper');
    if (e.detail.source == "touch") {
      this.setData({
        nowPage: e.detail.current
      })
    }
  },
  // 当点击底部导航栏时
  tabChange(e) {
    console.log(123)
    this.setData({
      nowPage: e.detail.index
    })
  },
  // 登陆事件
  onLoginEvent(e) {
    console.log(e);
    this.setData({
      userInfo: e.detail.userInfo,
      isLogin: true
    })
  }

})